package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Attraction;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.query.AttractionQuery;
import cn.wolfcode.luowowo.article.service.IAttractionService;
import cn.wolfcode.luowowo.cache.service.IAttractionStatisRedisService;
import cn.wolfcode.luowowo.comment.domain.AttractionComment;
import cn.wolfcode.luowowo.comment.query.AttractionCommentQuery;
import cn.wolfcode.luowowo.comment.service.IAttractionCommentService;
import cn.wolfcode.luowowo.comment.vo.UserVo;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/attraction")
public class AttractionController {
    @Reference
    private IAttractionService attractionService;
    @Reference
    private IAttractionStatisRedisService attractionStatisRedisService;
    @Reference
    private IAttractionCommentService attractionCommentService;
    @RequestMapping("")
    public String top5List(Model model, Long destId,@ModelAttribute("qo") AttractionQuery qo){
        //查info
        Destination dest = attractionService.getDestInfoByDestId(destId);
        //查前5篇
        List<Attraction> attrs = attractionService.top5ListByDestId(destId);
        //打印图片路径看看
//        System.out.println(attrs.get(0).getCoverUrlList());
        //分享过去
        model.addAttribute("list",attrs);
        model.addAttribute("dest",dest);


        //查询热门景点
        List<Attraction> hots = attractionService.getHot10();

        for (Attraction hot : hots) {
          hot.setCoverUrl(hot.getCoverUrlList().subList(0,1).get(0));
        }
        model.addAttribute("hots",hots);

        //查询所有景点分页
        PageInfo<Attraction> page = attractionService.list(qo);

        List<Attraction> pageList = page.getList();
        for (Attraction p : pageList) {
            p.setCoverUrl(p.getCoverUrlList().subList(0,1).get(0));
        }
        page.setList(pageList);
        //共享
        model.addAttribute("pageInfo",page);



        return  "/attraction/list";
    }

    @RequestMapping("/detail")
    public String detail(Model model, Long attrId,@UserParam UserInfo userInfo,@ModelAttribute("qo")AttractionCommentQuery qo){
        //查明显
        Attraction attraction = attractionService.getbyId(attrId);
        //查内部景点
        List<Attraction> inAttrs = attractionService.inAttrbyId(attrId);
        for (Attraction inAttr : inAttrs) {
            inAttr.setCoverUrl(inAttr.getCoverUrlList().subList(0,1).get(0));
        }
        model.addAttribute("attr",attraction);
        model.addAttribute("inAttrs",inAttrs);

        //查询景点去过   用户是否已经点击去过,
        if(userInfo!=null){
            Boolean rest = attractionStatisRedisService.selectUserList(attrId,userInfo.getId());
            model.addAttribute("isGo",rest);
        }

        //查询景点去过   用户是否已经点击去过,
        if(userInfo!=null){
            Boolean rest = attractionStatisRedisService.selectFavor(attrId,userInfo.getId());
            model.addAttribute("isFavor",rest);
        }

        //查询评论list 带分页
        Page comments = attractionCommentService.query(qo);
        model.addAttribute("page",comments);

        return "/attraction/detail";
    }
    @RequestMapping("/pagetpl")
    public String pagetpl(Model model,@ModelAttribute("qo")AttractionCommentQuery qo){
        Page comments = attractionCommentService.query(qo);
        model.addAttribute("page",comments);
        return "/attraction/commentTpl";
    }



    @RequestMapping("/go")
    @ResponseBody
    public Object go(Long aid, @UserParam UserInfo userInfo){
        if(userInfo == null){
            AjaxResult ajaxResult = new AjaxResult(false, "登录先");
            ajaxResult.setCode(102);
            return ajaxResult;
        }
        //去过获取取消去过
        Boolean rest = attractionStatisRedisService.addGo(aid,userInfo.getId());
        return new AjaxResult(rest,"");
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long aid, @UserParam UserInfo userInfo){
        if(userInfo == null){
            AjaxResult ajaxResult = new AjaxResult(false, "登录先");
            ajaxResult.setCode(102);
            return ajaxResult;
        }
        //去过获取取消去过
        Boolean rest = attractionStatisRedisService.addFavor(aid,userInfo.getId());
        return new AjaxResult(rest,"");
    }


    //评论
    @RequestMapping("/comment")
    @ResponseBody
    public Object comment(AttractionComment attractionComment,@UserParam UserInfo userInfo,String commentImgUrl[]){

        //将数组的图片用，号分开
        String join = StringUtils.join(commentImgUrl, ",");

        //如果没有登录请先登录
        if(userInfo == null){
            AjaxResult ajaxResult = new AjaxResult(false, "登录先");
            ajaxResult.setCode(102);
            return ajaxResult;
        }

        attractionComment.setCity(userInfo.getCity());
        attractionComment.setUserId(userInfo.getId());
        attractionComment.setUsername(userInfo.getNickname());
        attractionComment.setHeadUrl(userInfo.getHeadImgUrl());
        attractionComment.setLevel(userInfo.getLevel());
        attractionComment.setCommentUrl(join);
        attractionCommentService.saveOrUpdate(attractionComment);
        System.out.println(Arrays.toString(commentImgUrl));
        return AjaxResult.SUCCESS;
    }


    //查询评论list 带分页
    @RequestMapping("/commentList")
    @ResponseBody
    public Object commentList(@ModelAttribute("qo")AttractionCommentQuery qo){
        //qo.setAttrId(1L);
        Page comments = attractionCommentService.query(qo);
       return comments;
    }


    /**
     *
     * @param pid 一级评论 ObjectId
     * @param puserId  一级评论的用户id
     * @param pusername 一级评论的用户名
     * @param commentUserId  回复评论userid
     * @param commentUsername  回复评论 用户名
     * @param attrId     景点id
     * @param attrTitle   景点标题
     * @param commentContent   评论内容
     * @return
            form:{AttractionComment}
            to:{AttractionComment}
            content:xxx
            Date createTime; //创建时间
     */
    @RequestMapping("/commentReply")
    @ResponseBody
    public Object commentReply(@UserParam UserInfo userInfo, String commentContent,String pid,Long puserId,String pusername,Long commentUserId,String commentUsername,Long attrId,String attrTitle){
        AttractionComment comment = attractionCommentService.get(pid);//通过id查询一级评论
        System.out.println(comment.getReplay());

        //form
        String formUUID= UUID.randomUUID().toString().replaceAll("-", "");
        UserVo voForm = new UserVo();
        voForm.setId(formUUID);
        //voForm.setId(userInfo.getId());
        voForm.setUserId(userInfo.getId());
        voForm.setUsername(userInfo.getNickname());
        voForm.setHeadUrl(userInfo.getHeadImgUrl());

        //to
        String  toUUID= UUID.randomUUID().toString().replaceAll("-", "");
        UserVo voTo = new UserVo();
        //voTo.setId(puserId);
        voTo.setUserId(puserId);
        voTo.setId(toUUID);
        voTo.setUsername(pusername);


        HashMap<String, Object> map = new HashMap<>();
        map.put("form",voForm);
        map.put("to",voTo);
        map.put("content",commentContent);
        map.put("createTime",new Date());
        List<Map<String, Object>> replay = comment.getReplay();
        if (replay==null){
            List<Map<String, Object>> re = new ArrayList<>();
            re.add(map);
            replay = re;
        }else {
            replay.add(map);
        }
        comment.setReplay(replay);
        attractionCommentService.saveOrUpdate(comment);
        return AjaxResult.SUCCESS;
    }


}
